package net.cruciblesoftware.embers;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;


class LogicThread extends Thread {
    private static final int STATE_MIN = 0;
    public static final int STATE_INTRO = 1;
    public static final int STATE_RUNNING = 2;
    public static final int STATE_PAUSE = 3;
    public static final int STATE_LOSE = 4;
    public static final int STATE_WIN = 5;
    public static final int STATE_CLOSING = 6;
    private static final int STATE_MAX = 7;

    private boolean running = false;
    private int state = LogicThread.STATE_INTRO;
    private int oldState = state;

    private SurfaceHolder surface;
    private Scene scene;

    public LogicThread(SurfaceHolder sh) {
        Log.i("LOGIC", "creating thread");
        surface = sh;
    }

    public void setState(int newState) {
        if(newState > STATE_MIN && newState < STATE_MAX) {
            oldState = state;
            state = newState;
            if(state == STATE_INTRO || state == STATE_RUNNING)
                running = true;
        }
        else
            Log.w("LOGIC", "ignoring invalid state " + newState);
    }

    public void pause(boolean pausing) {
        if(pausing) {
            oldState = state;
            state = STATE_PAUSE;
        } else
            state = oldState;
    }

    /* Main loop
     * Must be called after surface is created 
     */
    @Override
    public void run() {
        Canvas c = null;
        scene = Scene.getInstance();
        long elapsedTime, currentTime, lastTime = System.currentTimeMillis();

        Log.i("LOGIC", "main loop running: " + running);
        while(running) {
            // time keeping
            currentTime = System.currentTimeMillis();
            elapsedTime = currentTime - lastTime;
            lastTime = currentTime;

            // manage game state
            switch(state) {
            case STATE_INTRO:
                state = STATE_RUNNING;
                break;
            case STATE_RUNNING:
                scene.update(elapsedTime);
                break;
            case STATE_PAUSE:
                break;
            case STATE_LOSE:
                state = STATE_CLOSING;
                break;
            case STATE_WIN:
                state = STATE_CLOSING;
                break;
            case STATE_CLOSING:
                running = false;
                break;
            case STATE_MIN:
            case STATE_MAX:
            default:
                Log.w("LOGIC", "thread running under invalid state!");
                Log.w("LOGIC", "\tstate=" + state + ", oldState=" + oldState);
                break;
            }

            // draw to the screen
            c = null;
            try {
                c = surface.lockCanvas(null);
                synchronized(surface) {
                    switch(state) {
                    case STATE_INTRO:
                        //intro.draw(c);
                        break;
                    case STATE_RUNNING:
                        scene.draw(c);
                        break;
                    }
                }
            } finally {
                if(c != null) surface.unlockCanvasAndPost(c);
            }
        }
        Log.i("LOGIC", "exiting main loop");
    }
}
